<template>
  <div class="cart-container">
    <div class="mui-card" v-for="(item,i) in cartlist" :key="item.id">
      <div class="mui-card-content">
        <div class="mui-card-content-inner">
          <mt-switch v-model="$store.getters.getGoodsSelected[item.id]"
          @change="selectedChanged(item.id,$store.getters.getGoodsSelected[item.id])"></mt-switch>
          <img :src="item.thumb_path" alt>
          <div class="goods-info">
            <h3>{{item.title}}</h3>
            <p class="selectedInfo">
              <span class="price">￥{{item.sell_price}}</span>
              <number-box :count="$store.getters.getGoodsCount[item.id]" :id="item.id"></number-box>
              <a href="#" @click.prevent="remove(item.id,i)">删除</a>
            </p>
          </div>
        </div>
      </div>
    </div>
    <div class="mui-card">
      <div class="mui-card-content">
        <div class="mui-card-content-inner">
          <div class="totalCount">
            <p>总计（不包含运费）</p>
            <p>已勾选商品
              <span class="highlight">{{$store.getters.getGoodsCountAndAmount.count}}</span>件，总价：
              <span class="highlight">￥{{$store.getters.getGoodsCountAndAmount.amount}}</span>
            </p>
          </div>
          <mt-button type="danger">去结算</mt-button>
        </div>
      </div>
    </div>
    <p>{{$store.getters.getGoodsSelected}}</p>
  </div>
</template>
<script>
import numberBox from "../common/ShopNum.vue";
export default {
  data() {
    return {
      value: true,
      cartlist: []
    };
  },
  methods: {
    getshopcartlist() {
      let idArr = [];
      this.$store.state.car.forEach(item => {
        idArr.push(item.id);
      });
      this.$http
        .get("api/goods/getshopcarlist/" + idArr.join(","))
        .then(res => {
          if (res.body.status === 0) {
            this.cartlist = res.body.message;
          }
        });
    },
    remove(id,index){
      this.cartlist.splice(index,1);
      this.$store.commit("removeFormCar",id)
    },
    selectedChanged(id,val){
      // console.log(id+'---'+val);
      this.$store.commit("updataGoodsSelected",{id,selected:val})
    }
  },
  components: {
    numberBox
  },
  created() {
    this.getshopcartlist();
  }
};
</script>
<style lang="scss" scoped>
.cart-container {
  background-color: #eee;
  overflow: hidden;
  .mui-card-content-inner {
    display: flex;
    align-items: center;
    justify-content: space-between;
    img {
      width: 60px;
    }
    .goods-info {
      display: flex;
      flex-direction: column;
      justify-content: space-between;
      h3 {
        font-size: 14px;
      }
      .selectedInfo {
        display: flex;
        align-items: center;
        justify-content: space-around;
        .price {
          color: red;
          font-size: 14px;
        }
      }
    }
  }
  .highlight {
    color: red;
    font-weight: 800;
  }
}
</style>
